package cn.edu.scujcc.customofsichuan;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;

import android.view.View;

import android.widget.Button;


public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    Button btn_home,btn_live,btn_explore,btn_mine,btn_tmp;
    explore explore;
    home home;
    mine mine;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //初始化底部导航栏四个按钮
        btn_home=findViewById(R.id.btn_home);
        btn_live=findViewById(R.id.btn_live);
        btn_explore=findViewById(R.id.btn_explore);
        btn_mine=findViewById(R.id.btn_mine);


        //添加监视器
        btn_home.setOnClickListener(this);
        btn_live.setOnClickListener(this);
        btn_explore.setOnClickListener(this);
        btn_mine.setOnClickListener(this);
        //初始化四个界面
        home=new home();
        explore=new explore();
        mine=new mine();

        //进入软件时立即加载首页
        replaceFragment(home);
        btn_home.setSelected(true);
        btn_tmp=btn_home;
        //直播界面是通过跳转
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_home:
                ButtonChanging(btn_tmp,home,btn_home);
                btn_tmp=btn_home;
                break;
            case R.id.btn_live:
                //跳转直播
                Intent intent=new Intent(MainActivity.this, video.class);
                startActivity(intent);
                break;
            case R.id.btn_explore:
                ButtonChanging(btn_tmp,explore,btn_explore);
                btn_tmp=btn_explore;
                break;
            case R.id.btn_mine:
                ButtonChanging(btn_tmp,mine,btn_mine);
                btn_tmp=btn_mine;
                break;

        }
    }


    private void replaceFragment(Fragment fragment){
        FragmentManager manager=getSupportFragmentManager();
        FragmentTransaction transaction=manager.beginTransaction();
        transaction.replace(R.id.fragement_background,fragment);
        //transaction.addToBackStack(null);//将之前点击的帧布局压栈
        transaction.commit();
    }


    //该方法实现底部按钮切换效果，四个按钮执行类似操作故将其封装，最后涉及到对象赋值操作，
    //而在方法内则执行的是地址的赋值，会导致代码无法正常运行，故将其剥离出来
    private void ButtonChanging(Button tmpButton,Fragment newFragement,Button nowButton){
        tmpButton.setSelected(false);//通过使用临时变量实现按钮切换效果
        tmpButton.setTextColor(Color.parseColor("#000000"));
        replaceFragment(newFragement);
        nowButton.setSelected(true);
        nowButton.setTextColor(Color.parseColor("#FFFFFF"));
    }

}